﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using libxl;
using System.Reflection;
using System.Collections;
using System.IO;
using Spire.Xls;
using System.Data;
using System.Xml;
using Newtonsoft.Json;

namespace ExcelTool
{

    public class ShareData
    {
        int length;
        Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
    }
    public class DataStructToExcel : EditorWindow
    {
        private static Book book = null;
        private static string sheetOneName = "";
        private static string sheetTwoName = "";
        private static string sheetThreeName = "";

        private static List<string> sheetName = new List<string>();


        [MenuItem(@"Tool/DataStructToExcel #&s")]
        private static void DataStructToExcelFunction()
        {

            List<Tcs> lst = new List<Tcs>();
            for (int i = 0; i < 10; ++i)
            {
                Tcs ts = new Tcs();
                ts.num1 = 1;
                ts.num2 = "2";
                ts.num3 = true;
                ts.num4 = 4;
                ts.num5 = 5;

                ts.ncl = new Ncl();
                ts.ncl.id = i;
                ts.ncl.nss2 = 33;

                ts.ncl2 = new Ncl2();
                ts.ncl2.id = i;
                ts.ncl2.nss2 = 2;
                ts.ncl2.nclst = new List<Ncl>();

                Ncl newClassss = new Ncl();
                newClassss.id = i;
                newClassss.nss2 = 11;
                ts.ncl2.nclst.Add(newClassss);


                Ncl2 newClass2 = new Ncl2();
                newClass2.id = i;
                newClass2.nss2 = 2;
                newClass2.nclst = new List<Ncl>();

                Ncl newClass = new Ncl();
                newClass.id = i;
                newClass.nss2 = 22;
                newClass2.nclst.Add(newClass);

                ts.nclst = new List<Ncl2>();
                ts.nclst.Add(newClass2);
                ts.nclst.Add(newClass2);
                ts.nclst.Add(newClass2);
                ts.nclst.Add(newClass2);
                lst.Add(ts);

            }
            SerializationTool.Serializer.Instance.Serialize<List<Tcs>>(lst);
            //             string xml = XmlUtil.Serializer(typeof(List<Tcs>), lst);
            //             Debug.Log(xml);
            //             if (File.Exists(Application.dataPath + "/../" + "xml.xml"))
            //                 File.Delete(Application.dataPath + "/../" + "xml.xml");
            //             Write(Application.dataPath + "/../" + "xml.xml", xml);
            // 
            // 
            //             XmlDocument myXmlDoc = new XmlDocument();
            //             myXmlDoc.LoadXml(xml);
            //             for(int i = 0;i < myXmlDoc.ChildNodes.Count;++i)
            //             {
            //                 XmlNode node = myXmlDoc.ChildNodes[i];
            //                 int count = node.ChildNodes.Count;
            //                 for (int j = 0; j < count; ++j)
            //                 {
            //                     XmlNode nodeJ = node.ChildNodes[j];
            //                     Debug.Log(node.ChildNodes[j].Name);
            //                     for (int k = 0; k < nodeJ.ChildNodes.Count; ++k)
            //                     {
            //                         Debug.Log(nodeJ.ChildNodes[k].Name);
            //                     }
            //                 }
            //             }

            //             ExcelUtility excel = new ExcelUtility(Application.dataPath + "/../" + "out.xlsx");
            //             excel.ConvertToXml(Application.dataPath + "/../" + "xml.xml");
            // 
            //             string fileName = Spire.License.LicenseProvider.GetLicenseFileName();
            //             FileInfo licenseFile = new FileInfo(fileName);
            //             Spire.License.LicenseProvider.SetLicenseFile(licenseFile);
            // 
            //             Workbook workbook = new Workbook();
            //             workbook.LoadFromFile("out.xlsx", ExcelVersion.Version2010);
            //             workbook.SaveAsXml("D:\\result.xml");
            // 
            //             Workbook workbook1 = new Workbook();
            //             workbook1.LoadFromXml("result.xml");
            //             workbook1.SaveToFile("test.xls", ExcelVersion.Version2010);

            //Create a stream to serialize the object to.  
            //             MemoryStream ms = new MemoryStream();
            // 
            //             // Serializer the User object to the stream.  
            //             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(User));
            //             ser.WriteObject(ms, user);
            //             byte[] json = ms.ToArray();
            //             ms.Close();
            //             return Encoding.UTF8.GetString(json, 0, json.Length);
            //             List<TestClassStruct> dt2 = XmlUtil.Deserialize(typeof(List<TestClassStruct>), xml) as List<TestClassStruct>;
            //             Debug.Log("dd");
            // 
            //             DataTable dtStruct = XmlUtil.Deserialize(typeof(DataTable), xml) as DataTable;
            //             Debug.Log("dd");
        }

    }
}

